<template>
  <z-modal
    :width="800"
    v-bind="modalOption"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model layout="horizontal" labelAlign="right" ref="ruleForm" :label-col="labelCol" :wrapper-col="wrapperCol" :model="form" :rules="validatorRules">
        <a-form-model-item label="职系编码" prop="code" >
          <a-input placeholder="请输入职系编码" v-model="form.code" />
        </a-form-model-item>
        <a-form-model-item label="职系名称" prop="name">
          <a-input placeholder="请输入职系名称" v-model="form.name" />
        </a-form-model-item>
        <!-- <a-form-model-item label="描述">
          <a-textarea placeholder="请输入描述" v-model="form.desc" :rows="3" />
        </a-form-model-item>
        <a-form-model-item label="状态">
          <a-radio-group v-model="form.status" buttonStyle="solid">
            <a-radio-button value="RUNNING">生效</a-radio-button>
            <a-radio-button value="PAUSE">失效</a-radio-button>
          </a-radio-group>
        </a-form-model-item> -->
      </a-form-model>
    </a-spin>
  </z-modal>
</template>

<script>
import ModalFormMixin from '@/mixins/ModalFormMixin'
import debounce from 'lodash/debounce'
import { duplicateCheck } from '@/services/common'
import { CURY_TYPE } from '@/store/mutation-types'
export default {
  name: 'PostTypeModal',
  mixins: [ModalFormMixin],
  components: {

  },
  data() {
    return {
      buttonStyle: 'solid',
      validatorRules: {
        code: [
          { required: true, message: '请输入职系编码' },
          { validator: (rule, value, callback) => this.validateDuplicate(rule, value, callback), trigger: 'change' }
        ],
        name: [{ required: true, message: '请输入职系名称' }]
      },
      url: {
        add: '/posttype/add',
        edit: '/posttype/edit'
      }
    }
  },
  methods: {
    addAfter() {
      // this.$set(this.form, 'status', 'RUNNING')
    },
    classifyIntoFormData() {
      this.form.crudType = this.editType === 'add' ? CURY_TYPE.SAVE : CURY_TYPE.UPDATE
      return [
        {
          ...this.form
        }
      ]
    },
    validateDuplicate: debounce(function(rule, value, callback) {
      if (!value) {
        callback()
      } else {
        duplicateCheck({
          tableName: 'mb_post_type',
          column: 'code_',
          key: value,
          exclude: this.editType === 'edit' ? value : ''
        }).then(res => {
          if (res.success) {
            res.result === 0 ? callback() : callback('编码已存在')
          } else {
            this.$message.warning(res.message)
          }
        })
      }
    }, 400)
  }
}
</script>

<style scoped>

</style>
